package cn.com.trueway.proxy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AOPDemoProxy implements Proxy{

    private final String execPoint = "execution(* cn.com.trueway.service.AOPDemoService.* (..))";

    @Around(value = execPoint)
    @Override
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("logAround方法执行了");
        String name = joinPoint.getSignature().getName();       //获取方法名
        Object[] args = joinPoint.getArgs();                    //获取参数
        return joinPoint.proceed();                             //放行
    }

    @After(value = execPoint)
    @Override
    public void logAfter(JoinPoint joinPoint){
        System.out.println("logAfter方法执行了");
    }

    @Before(value = execPoint)
    @Override
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("logBefore方法执行了");
        String name = joinPoint.getSignature().getName();   //获取即将执行的方法
        Object[] args = joinPoint.getArgs();                //获取参数

    }

    @AfterReturning(value = execPoint,returning = "result")
    @Override
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        System.out.println("logAfterReturning方法执行了");
        System.out.println("方法返回：" + result);
    }

    @AfterThrowing(value = execPoint,throwing = "exception")
    @Override
    public void logAfterThrowing(JoinPoint joinPoint, Exception exception) {
        System.out.println("logAfterThrowing方法执行了");
        System.out.println("方法异常：" + exception);
    }
}
